package nguyen.huu.phong.slidetext;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.GlyphVector;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.sun.awt.AWTUtilities;

public class SlideText extends JWindow {

	private static final long serialVersionUID = -7531083634905547422L;
	
	private JLabel overlay;
	
	private Content content;
	
	private Configure configure;
	
	private Point mousePoint;

	public SlideText() {
		configure = Configure.getInstance();
		overlay = new JLabel(configure.title){

			private static final long serialVersionUID = -8343175578953526338L;
			
//			public void paint(Graphics g) {
//				Graphics2D g2 = (Graphics2D) g;
//				g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//				g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,	RenderingHints.VALUE_ANTIALIAS_ON);
//				g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
//				g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
//				g2.translate(0, getPreferredSize().height - g2.getFontMetrics().getDescent());
//				Shape shape = getShape(g2, getText());
//				g2.setColor(configure.color);
//				g2.fill(shape);
//				g2.setColor(Color.BLUE);
//				g2.setStroke(new BasicStroke(1.0f));
//				g2.draw(shape);
//			}
			
			private Shape getShape(Graphics2D g2, String text){
				GlyphVector gv = getFont().createGlyphVector(g2.getFontRenderContext(), text);
				return gv.getOutline();
			}
		};
		Container container = getContentPane();
		container.setLayout(new BorderLayout());
		container.add(overlay, BorderLayout.CENTER);
		overlay.setFont(overlay.getFont().deriveFont(Font.BOLD, configure.fontSize));
		overlay.setForeground(configure.color);
		overlay.setComponentPopupMenu(getPopupMenu());
		overlay.addMouseMotionListener(new MouseAdapter() {
			public void mouseDragged(MouseEvent e) {
					Point current = e.getLocationOnScreen();
					configure.location = new Point(current.x - mousePoint.x, current.y - mousePoint.y);
					SlideText.this.setLocation(configure.location);
					SlideText.this.setAlwaysOnTop(true);
					configure.save();
			}
		});
		overlay.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				mousePoint = e.getPoint();
			}
		});
		
		try {
			content = new Content(configure.defaultFolder);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		new Thread(){
			public void run(){
				while (true){
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							try {
								overlay.setText(content.getContent());
								SlideText.this.setSize(overlay.getPreferredSize());
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					});
					try {
						Thread.sleep(configure.sleepTime);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
//		new Thread(){
//			public void run(){
//				while (true){
//					Point location = MouseInfo.getPointerInfo().getLocation();
//					location.translate(-(overlay.getPreferredSize().width / 2), 10);
//					SlideText.this.setLocation(location);
//					SlideText.this.setAlwaysOnTop(true);
//					try {
//						Thread.sleep(5);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//		}.start();
		AWTUtilities.setWindowOpaque(this, false);
		setAlwaysOnTop(true);
		setSize(configure.width, configure.height);
		setLocation(configure.location);
		setVisible(true);
	}
	
	private JPopupMenu getPopupMenu(){
		JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem itemSelectFolder = new JMenuItem("Select folder...");
		JMenuItem itemConfigure = new JMenuItem("Select color...");
		JMenuItem itemExit = new JMenuItem("Exit");
		itemExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				configure.save();
				System.exit(0);
			}
		});
		
		popupMenu.add(itemSelectFolder);
		popupMenu.add(itemConfigure);
		popupMenu.addSeparator();
		popupMenu.add(itemExit);
		return popupMenu;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (Exception e) {
					e.printStackTrace();
				}
				new SlideText();
			}
		});
		
	}
}
